package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import user.Conference;
import user.Role;
import user.User;
import user.UserCollection;
import document.Manuscript;
import document.ManuscriptCollection;

/**
 * test main frame with hardcoded user.
 * @author Tony Butterfield
 * @author Christopher Yeaman(Edited several methods and JavaDoc
 * @version 12/4/2011
 */
@SuppressWarnings("serial")
public class MainFrame1 extends JFrame
{
  //STATIC FIELDS ****************************************
  /** default width of the main frame. */
  public static final int WIDTH = 1100;

  /** default height of the drawing panel. */
  public static final int HEIGHT = 700;

  // INSTANCE FIELDS **************************************
  /** PCViewsPanel.  */
  protected PCViewsPanel2 my_panel1;

  /** PCDecisionPanel.   */
  protected PCDecisionPanel my_panel2;

  /** AuthorSubmitsPanel.  */
  protected AuthorSubmitsPanel my_panel3;
  
  /** SidebarPanel.  */
  private SideBarPanel my_panel0;

  /** currently selected conference. */
  private Conference my_current_conference;

  /** currently selected User. */
  private User my_current_user;

  /** currently selected Role. */
  private Role my_current_role;

  /** currently selected Manuscript.  */
  private Manuscript my_current_manuscript;
   
  /** path to persistent storage folder.  */
  // could be a static, but want to read in from config.txt eventually
  private final String my_storage_path = "PersistentStore/"; 

  /** UserCollection class. */
  private final UserCollection my_users;

  /** ManuscriptCollection class. */
  private final ManuscriptCollection my_manuscripts;
  


  // CONSTRUCTOR ******************************************
  /**
   * main frame for user story 1.
   * @author Tony Butterfield and Christopher Yeaman
   */
  public MainFrame1()
  {
    super();
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    
    my_users = new UserCollection();
    my_manuscripts = new ManuscriptCollection(my_users);

    try
    {
      setUpData();
    }
    catch (final IOException e)
    {
      JOptionPane.showMessageDialog(this, "Woops!!! I threw an IOException!");
    }
    
    my_current_user = my_users.getUser("Answer, Harry");
    my_current_conference = my_current_user.getConference("Code Generation 2012");
    my_current_role = my_current_user.getRole(my_current_conference);
    my_current_manuscript = 
        my_manuscripts.getManuscriptByTitle("Everytime you code a GOTO--God kills a kitten " +
            "(and other myths)");
        
    //TODO: debug code to remove
    if (my_current_user == null || my_current_conference == null || my_current_role == null)
    {
      JOptionPane.showMessageDialog(this, "Dammit! Something didn't load!");
    }

    setUpGUI();
    refresh();
    pack();
  }

  
  // ACCESSORS ********************************************
  /**
   * get reference to UserCollection.
   * @return UserCollection
   */
  public UserCollection getUserCollection()
  {
    return my_users;
  }
  
  /**
   * get reference to ManuscriptCollection.
   * @return ManuscriptCollection
   */
  public ManuscriptCollection getManuscriptCollection()
  {
    return my_manuscripts;
  }
  
  /**
   * get current user.
   * @return User currently selected
   */
  public User getCurrentUser()
  {
    return my_current_user;
  }
  
  /**
   * get current conference.
   * @return Conference currently selected
   */
  public Conference getCurrentConference()
  {
    return my_current_conference;
  }
  
  /**
   * get current role.
   * @return Role currently selected
   */
  public Role getCurrentRole()
  {
    return my_current_role;
  }
  
  /**
   * Retrieves the current manuscript selected.
   * 
   * @return The current manuscript.\
   * @author Christopher Yeaman
   */
  public Manuscript getCurrentManuscript()
  {
    return my_current_manuscript;
  }

  
  // MUTATORS *********************************************
  /**
   * Sets the current user and it's role in the conference.
   * 
   * @param the_user The new user.
   * @author Christopher Yeaman, <b>Editing by:</b> Tony Butterfield
   */
  public void setCurrentUser(final User the_user)
  {
    my_current_user = the_user;

    if (my_current_user.getRole(my_current_conference).equals(null))
    {
      my_current_role = Role.AUTHOR;      
    }
    else 
    {
      my_current_role = my_current_user.getRole(my_current_conference);
    }

    JOptionPane.showMessageDialog(this, "user: " + my_current_user.getFullName() + "\n" +
        "conference: " + my_current_conference.toString() + "\n" + "Role: " +
        my_current_role);

    refresh();
    pack();
  }

  /**
   * set new current conference.
   * @param the_conference Conference from combobox
   */
  public void setCurrentConference(final Conference the_conference)
  {
    my_current_conference = the_conference;
    
    if (my_current_user.getRole(my_current_conference).equals(null))
    {
      my_current_role = Role.AUTHOR;      
    }
    else 
    {
      my_current_role = my_current_user.getRole(my_current_conference);
    }
    
    JOptionPane.showMessageDialog(this, "user: " + my_current_user.getFullName() + "\n" +
        "conference: " + my_current_conference.toString() + "\n" + "Role: " +
        my_current_role);
    
    refresh();
    pack();
  }

  // METHODS *****************************
  /**
   * load all test data for the two collections.
   * @throws IOException
   */
  private void setUpData() throws IOException
  {
    my_users.loadAllTestData();
    my_manuscripts.loadAllTestData();
  }
  
  /**
   * update title bar and call refresh for all panels.
   */
  public void refresh()
  {
    this.setTitle("ManuscriptPR - Conference: " + my_current_conference.toString() + "     " +
        "User: " + my_current_user.getFullName() + " / " +
        my_current_user.getRole(my_current_conference));
    my_panel1.refresh();
    //my_panel2.refresh();
    this.repaint();
    pack();
  }
  
  /**
   * load all gui elements.
   */
  private void setUpGUI()
  {    
    my_panel0 = new SideBarPanel(this);
    my_panel1 = new PCViewsPanel2(this);
    my_panel2 = new PCDecisionPanel(my_current_manuscript, this);
    //my_panel2.setVisible(false);
    add(my_panel0, BorderLayout.WEST);
    
    //add(my_panel2, BorderLayout.CENTER);
    //add(my_panel1, BorderLayout.CENTER);
    
    
    
    refresh();
    pack();
  }
}
